package com.tripupdater.model
{
	import com.tripupdater.vo.CalendarPlaceVO;
	
	import flash.events.EventDispatcher;
	
	/**
	 * Singleton
	 */
	public class Model extends EventDispatcher
	{
		private static var _allowInstantiation:Boolean= false;
		private static var _instance:Model= null;
		
		/**
		 * TwitterVO[]
		 */
		public var twitterStatusTimeline:Array;
		
		/**
		 * TwitterVO[]
		 */
		public var altTwitterStatusTimeline:Array;
		
		/**
		 * CalendarPlaceVO[]
		 */
		public var calendarList:Array;
		
		public function Model()
		{
			super();
			
			if(!_allowInstantiation){
				throw new Error("## TripUpdater - Singleton error. Use getInstance() method");
			}
		}
		
		public static function getInstance():Model
		{
			if(_instance == null){
				_allowInstantiation= true;
				_instance= new Model();
				_allowInstantiation= false;
			}
			
			return _instance;
		}
		
		public function getCurrentPosition():String
		{
			var today:Date= new Date();
			var currentPos:CalendarPlaceVO= calendarList[0] as CalendarPlaceVO;
			var ret:String= "";
			for(var i:int= 0; i < calendarList.length; i++){
				var place:CalendarPlaceVO= calendarList[i] as CalendarPlaceVO;
				if(today.getTime() < place.date.getTime()){
					currentPos= place;
					break;
				}
			}
			
			ret+= (currentPos.location.length > 1)?currentPos.location:"";
			ret+= (currentPos.city.length > 1)?((ret == "")?currentPos.city:(", " + currentPos.city + ", ")):"";
			ret+= currentPos.country; 
			
			return ret;
		}
		
		public function getRelativeTime(time:Date):String {
 			var fromTime:Date = new Date(time);
 			var toTime:Date = new Date();
 			var returnValue:String;
 			var differenceInSeconds:int = (toTime.getTime() - fromTime.getTime())/1000;
 			var differenceInMinutes:int = differenceInSeconds / 60;
 			
			switch (true) {
				case(differenceInSeconds <= 50):
					returnValue = "Hace " + differenceInSeconds.toString() + " segundos.";
					break;
				case (differenceInSeconds > 50 && differenceInSeconds <= 90):
					returnValue = "Hace un minuto.";
					break;
				case (differenceInMinutes <= 50):
					returnValue = "Hace " + differenceInMinutes.toString() + " minutos.";
					break;
				case (differenceInMinutes > 50 && differenceInMinutes <= 110):
					returnValue = "Hace una hora.";
					break;
				case (differenceInMinutes > 110 && differenceInMinutes <= 1440):
					returnValue = "Hace " + int(differenceInMinutes/60).toString() + " horas.";
					break;
				case (differenceInMinutes > 1440 && differenceInMinutes <= 2880):
					returnValue = "Ayer.";
					break;
				case (differenceInMinutes > 2880 && differenceInMinutes <= 43200):
					returnValue = "Hace " + int(differenceInMinutes/1440).toString() + " días.";
					break;
				case (differenceInMinutes > 43200 && differenceInMinutes <= 86400):
					returnValue = "Hace un mes.";
					break;
				case (differenceInMinutes > 86400 && differenceInMinutes <= 525600):
					returnValue = "Hace " + int(differenceInMinutes/43200).toString() + " meses.";
					break;
				case (differenceInMinutes > 525600 && differenceInMinutes <= 1051200):
					returnValue = "Hace un año.";
					break;
				case (differenceInMinutes > 1051200):
					returnValue = "Hace " + int(differenceInMinutes/525600).toString() + " años.";
					break;
				default:
					returnValue = "";
			}
			
			return returnValue;
		}
	}
}